package service

import (
	"context"
	"github.com/gogf/gf/v2/net/ghttp"
	"websocket/internal/consts"
	"websocket/internal/model"
)

type (
	sContext struct{}
)

var (
	insContext = sContext{}
)

func Context() *sContext {
	return &insContext
}

func (s *sContext) InitAuth(r *ghttp.Request, customCtx *model.ContextAuth) {
	r.SetCtxVar(consts.ContextCustomAuthKey, customCtx)
}

func (s *sContext) GetAuth(ctx context.Context) *model.ContextAuth {
	value := ctx.Value(consts.ContextCustomAuthKey)
	if value == nil {
		return nil
	}
	if localCtx, ok := value.(*model.ContextAuth); ok {
		return localCtx
	}
	return nil
}

func (s *sContext) InitLogger(r *ghttp.Request, LoggerCtx *model.ContextLogger) {
	r.SetCtxVar(consts.ContextLoggerKey, LoggerCtx)
}

func (s *sContext) GetLogger(ctx context.Context) *model.ContextLogger {
	value := ctx.Value(consts.ContextLoggerKey)
	if value == nil {
		return nil
	}
	if localCtx, ok := value.(*model.ContextLogger); ok {
		return localCtx
	}
	return nil
}
